import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

/**
 * @作者： @wan005
 * @时间： 2023/3/3
 **/

public class jdbc_Certification_Case0003 {

    static public String driver = "org.opengauss.Driver";
	static public String dbuser;
    public static Properties getConfigFromFile(String filePath) {
        Properties props = new Properties();
        try {
            BufferedInputStream config = new BufferedInputStream(new FileInputStream(filePath));
            props.load(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return props;
    }

    public static String genURLFromPro(Properties props) {
        String hostname = props.getProperty("hostname");
        String[] hostnames = hostname.split(",");
        String port = props.getProperty("port");
        String[] ports = port.split(",");
        String dbname = props.getProperty("dbname");
		dbuser = props.getProperty("user");
        return genURLFromHostsPortsDBname(hostnames, ports, dbname);
    }

    public static String genURLFromHostsPortsDBname(String[] hostnames, String[] ports, String dbname) {
        String sourceURL;
        if (hostnames.length == 1) {
            sourceURL = "jdbc:opengauss://" + hostnames[0] + ":" + ports[0] + "/" + dbname;
        } else {
            ArrayList<String> ip_port_pare = new ArrayList<>();
            if (ports.length == 1) {
                for (String ip : hostnames) {
                    ip_port_pare.add(ip + ":" + ports[0]);
                }
            } else {
                for (int i = 0; i < hostnames.length; i++) {
                    ip_port_pare.add(hostnames[i] + ":" + ports[i]);
                }
            }
            sourceURL = "jdbc:opengauss://" + String.join(",", ip_port_pare) + "/" + dbname;
        }
        return sourceURL;
    }

    public static Connection GetConnection(Properties props) {
        Connection connR;
        String sourceURL = genURLFromPro(props);
        System.out.println(sourceURL);
        try {
            Class.forName(driver).newInstance();
            connR = DriverManager.getConnection(sourceURL, props);
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
        return connR;
    }

    public static void main(String[] args) {
        Properties pros = new Properties();
        Properties pros_conf = null;
        String jdbc_config_file = null;
        for (int i = 0; i < args.length; i++) {
            switch (args[i]) {
                case "--config-file":
                    jdbc_config_file = args[i + 1];
                    pros_conf = getConfigFromFile(jdbc_config_file);
                    break;
                case "-F":
                    jdbc_config_file = args[i + 1];
                    pros_conf = getConfigFromFile(jdbc_config_file);
                    break;
                default:
                    break;
            }
        }
        //建立连接
        if (pros_conf != null){
            pros_conf.putAll(pros);
        }
        Connection conn = GetConnection(pros_conf);
        System.out.println(conn);
        if(conn == null){
            System.out.println("连接失败");
            return;
        }else{
            try{
                String sql = "show enable_incremental_checkpoint;" + 
                             "show pagewriter_thread_num;" +
                             "show enable_default_ustore_table;" +
                             "show mot_config_file;" +
                             "select * from gs_redo_stat;" +
                             "select * from gs_wlm_user_info;" +
                             "select * from gs_db_privilege;" +
                             "select * from gs_auditing;" +
                             "select * from information_schema.schemata;" +
                             "select * from GS_OS_RUN_INFO;" +
                             "select gs_wlm_user_resource_info('" + dbuser + "');" +
                             "select gs_encrypt('openGauss','Asdf1234','sm4');" +
                             "select gs_encrypt('openGauss','Asdf1234','aes128');" +
                             "select gs_gsc_clean();" +
                             "select mot_global_memory_detail();" +
                             "select mot_local_memory_detail();" +
                             "select mot_session_memory_detail();";
                Statement stmt=conn.createStatement();
                boolean hasResultSet = stmt.execute(sql);
                do {
                    if (hasResultSet) {
                        ResultSet rs = stmt.getResultSet();
                        while (rs.next()){
                            System.out.println("值为:" + rs.getString(1));
                        }
                        hasResultSet = stmt.getMoreResults();
                    } else {
                        System.out.println("ERROR:未查询到值");
                    }
                } while (hasResultSet);

                sql = "select gs_encrypt('openGauss','Asdf1234','sm4');" +
                      "select gs_encrypt('openGauss','Asdf1234','aes128');";
                String[] encrypt_str = {"", ""};
                hasResultSet = stmt.execute(sql);
                int n = 0;
                do {
                    if (hasResultSet) {
                        ResultSet rs = stmt.getResultSet();
                        while (rs.next()){
                            System.out.println("加密值为:" + rs.getString(1));
                            encrypt_str[n] = rs.getString(1);
                        }
                        hasResultSet = stmt.getMoreResults();
                    } else {
                        System.out.println("ERROR:gs_encrypt加密函数执行失败");
                    }
                    n = n + 1;
                } while (hasResultSet);
                sql = "select gs_decrypt('" + encrypt_str[0] + "','Asdf1234','sm4');" +
                      "select gs_decrypt('" + encrypt_str[1] + "','Asdf1234','aes128');";
                hasResultSet = stmt.execute(sql);
                do {
                    if (hasResultSet) {
                        ResultSet rs = stmt.getResultSet();
                        while (rs.next()){
                            System.out.println("解密值为:" + rs.getString(1));
                        }
                        hasResultSet = stmt.getMoreResults();
                    } else {
                        System.out.println("ERROR:gs_decrypt解密函数执行失败");
                    }
                } while (hasResultSet);
            }catch (SQLException s){
                s.printStackTrace();
            }
        }
    }
}